package com.yangming.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yangming.common.base.Page;
import com.yangming.mapper.EmptyCartServiceMapper;
import com.yangming.mapper.EmptyCartServiceRouteMapper;
import com.yangming.model.EmptyCartService;
import com.yangming.service.IEmptyCartServiceService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 空板车服务实现类
 * @author PengShuiMing
 * @create 2018-09-07
 **/
@Service
public class EmptyCartServiceImpl implements IEmptyCartServiceService {
    @Autowired
    private EmptyCartServiceMapper emptyCartServiceMapper;
    @Autowired
    private EmptyCartServiceRouteMapper emptyCartServiceRouteMapper;
    @Override
    @Transactional
    public int saveEmptyCartService(EmptyCartService emptyCartService) {
        emptyCartService.setCreateTime(new Date());
        int i = emptyCartServiceMapper.insertSelective(emptyCartService);
        if(i>0&&emptyCartService.getRoute()!=null&&emptyCartService.getRoute().size()>0){
                Map<String, Object> params = new HashMap<String, Object>();
                params.put("emptyCartServiceId",emptyCartService.getId());
                params.put("list",emptyCartService.getRoute());
                i = emptyCartServiceRouteMapper.batchInsert(params);
        }
        return i;
    }

    @Override
    public Map<String,Object> selectListByPage(Map<String, Object> params,Page page) {
        Map<String,Object> resultMap = new HashMap<String,Object>();
        //分页查询
        PageHelper.startPage(page.getPageNo(),page.getPageSize());
        List<EmptyCartService> emptyCartServices = emptyCartServiceMapper.selectListByPage(params);
        PageInfo<EmptyCartService> pageInfo = new PageInfo<EmptyCartService>(emptyCartServices);
        resultMap.put("data",emptyCartServices);//数据集
        resultMap.put("total",pageInfo.getTotal());//总数据量
        resultMap.put("pageNo",pageInfo.getPageNum());//当前页
        resultMap.put("pageSize",pageInfo.getPageSize());//每页的数量
        resultMap.put("pages",pageInfo.getPages());//总页数
        resultMap.put("hasNetPage",pageInfo.isHasNextPage());//是否有下一页
        return resultMap;
    }

    @Override
    public EmptyCartService selectOne(Integer id) {
        EmptyCartService emptyCartService = emptyCartServiceMapper.selectOne(id);
        return emptyCartService;
    }
}
